package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.core.page.TableDataApi;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.DiscountHouseSigningorder;
import com.ruoyi.yipaiju.domain.ForeclosedHouseSigningorder;
import com.ruoyi.yipaiju.domain.HouseUploadingOrder;
import com.ruoyi.yipaiju.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/signingorders")
public class SigningordersController extends BaseController {

    @Autowired
    private IForeclosedHouseSigningorderService foreclosedHouseSigningorderService;
    @Autowired
    private IDiscountHouseSigningorderService discountHouseSigningorderService;
    @Autowired
    private IHouseUploadingOrderService houseUploadingOrderService;

    @Autowired
    private IXcxUserService xcxUserService;

    @Autowired
    private IValueAddedSigningorderService valueAddedSigningorderService;

    // TODO 差个分页不知道咋整  闹腾
    /**
     * 我的订单 签约订单  0全部 类型1 优选，类型2 折扣，类型3 放盘，类型4 增值
     * @param ordertype
     * @param oStage
     * @return
     */
    @GetMapping("")
    public TableDataApi list(Integer ordertype, Integer oStage) {

//        startPageApi();

        List<Object> list = new ArrayList<>();
        oStage = (oStage == null || oStage == 0) ? null : oStage;
        ordertype = ordertype == null ? 0 : ordertype;

        // 0全部 类型1 优选，类型2 折扣，类型3 放盘，类型4 增值  好像没有类型4呢。。。
        switch (ordertype){
            case 0:
                list.addAll(signingordersQueryType1(oStage));
                list.addAll(signingordersQueryType2(oStage));
                list.addAll(signingordersQueryType3(oStage));
                break;
            case 1:
//                fo_foreclosed_house_signingorder
                list.addAll(signingordersQueryType1(oStage));
                break;
            case 2:
//                fo_discount_house_signingorder
                list.addAll(signingordersQueryType2(oStage));
                break;
            case 3:
                // 放盘 fo_house_uploading_order  TODO 不确定是不是这个表
                list.addAll(signingordersQueryType3(oStage));
                break;
//            case 4:
//                list.addAll(signingordersQueryType4(oStage));
//                break;
        }

        return getTableApi(list);
    }

    public List<ForeclosedHouseSigningorder> signingordersQueryType1(Integer oStage){
        startPageApi();
        List<ForeclosedHouseSigningorder> foreclosedHouseSigningorders = foreclosedHouseSigningorderService.selectForeclosedHouseSigningorderList1(Builder.of(ForeclosedHouseSigningorder::new)
                .with(ForeclosedHouseSigningorder::setClient, SecurityUtils.getUserId())
                .with(ForeclosedHouseSigningorder::setoStage, oStage).build());
        List<Long> ids = foreclosedHouseSigningorders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        foreclosedHouseSigningorders.stream().forEach(item -> {
            if (item.getMarketer() != null){
                XcxUser xcxUser = xcxUserMap.get(item.getMarketer());
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(1);
        });
        return foreclosedHouseSigningorders;
    }

    public List<DiscountHouseSigningorder> signingordersQueryType2(Integer oStage){
        startPageApi();
        List<DiscountHouseSigningorder> discountHouseSigningorders = discountHouseSigningorderService.selectDiscountHouseSigningorderList1(Builder.of(DiscountHouseSigningorder::new)
                .with(DiscountHouseSigningorder::setClient, SecurityUtils.getUserId())
                .with(DiscountHouseSigningorder::setoStage, oStage).build());
        List<Long> ids = discountHouseSigningorders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        discountHouseSigningorders.stream().forEach(item -> {
            if (item.getMarketer() != null){
                XcxUser xcxUser = xcxUserMap.get(item.getMarketer());
                if (xcxUser != null){
                    item.setImg(xcxUser.getAvatarUrl());
                    item.setMarkName(xcxUser.getNickname());
                    item.setTelphone(xcxUser.getPhone());
                }
            }
            item.setType(2);
        });
        return discountHouseSigningorders;
    }

    public List<HouseUploadingOrder> signingordersQueryType3(Integer oStage){
        startPageApi();
        List<HouseUploadingOrder> houseUploadingOrders = houseUploadingOrderService.selectHouseUploadingOrderList1(Builder.of(HouseUploadingOrder::new)
                .with(HouseUploadingOrder::setMarketer, SecurityUtils.getUserId())
                .with(HouseUploadingOrder::setoStage, oStage).build());
        List<Long> ids = houseUploadingOrders.stream().filter(item -> item.getMarketer() !=null).map(item -> item.getMarketer()).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        houseUploadingOrders.stream().forEach(item -> {
            XcxUser xcxUser = xcxUserMap.get(item.getMarketer());
            if (xcxUser != null){
                item.setImg(xcxUser.getAvatarUrl());
                item.setMarkName(xcxUser.getNickname());
                item.setTelphone(xcxUser.getPhone());
            }
            item.setType(3);
        });
        return houseUploadingOrders;
    }

//    public List<ValueAddedSigningorder> signingordersQueryType4(Integer stage){
//        List<ValueAddedSigningorder> valueAddedSigningorders = valueAddedSigningorderService.selectValueAddedSigningorderList(Builder.of(ValueAddedSigningorder::new)
//                .with(ValueAddedSigningorder::setClient, SecurityUtils.getUserId().toString())
//                .with(ValueAddedSigningorder::setStage, stage).build());
//        valueAddedSigningorders.stream().forEach(item -> {item.setType(4);});
//        return valueAddedSigningorders;
//    }




}
